<?php
// 本类由系统自动生成，仅供测试用途
require COMMON_PATH.'logTool.class.php';
require COMMON_PATH.'message.class.php';
class IndexAction extends Action {

    public function index(){
        $user = isset($_SESSION['user'])?$_SESSION['user']:'';
        logTool::logCat('session: '.$user);
        if (empty($user)){
            $this->error("请先登录",U("index/login"));
        } else {
            $list = $this->getOrderNum();
            $this->assign('order',$list);
            $this->display();
        }
    }

    public function logout(){
        unset($_SESSION['user']);
        unset($_SESSION['rule']);
        $this->success('登出成功',U('index/login'));
    }

    //获取所有预约数量
    private function getOrderNum(){
        //所有预约
        $all_sql = "select count(id) from order_list where is_delete = 1";
        //已处理预约
        $done_sql = "select count(id) from order_list where status = 1 and is_delete = 1";
        //未处理预约
        $undo_sql = "select count(id) from order_list where status = 0 and is_delete = 1";

        $order = M('order_list');
        $all_list = $order->query($all_sql);
        $done_list = $order->query($done_sql);
        $undo_list = $order->query($undo_sql);
        $data = array(
            'all' => $all_list[0]['count(id)']?$all_list[0]['count(id)']:0,
            'done' => $done_list[0]['count(id)']?$done_list[0]['count(id)']:0,
            'undo' => $undo_list[0]['count(id)']?$undo_list[0]['count(id)']:0
        );
        return $data;
    }

    public function login(){
        $this->display();
    }

    public function check(){
        $user = isset($_REQUEST['user'])?$_REQUEST['user'].trim():'';
        $password = isset($_REQUEST['password'])?$_REQUEST['password'].trim():'';
        if (empty($user) || empty($password))
            $this->error("用户名或密码填写不正确，请重新输入");
        $password = md5($password);
        $account = M('user');
        $where = array(
            'user' => $user,
            'password' => $password,
            'is_delete' => 1,
            'status' => 1
        );
        if ($data = $account->where($where)->find()){
            $this->success("登录成功",U("index/index"));
            $_SESSION['user'] = $data['user'];
            $_SESSION['rule'] = $data['rule'];
        } else {
            $this->error("登录失败，请联系管理员");
        }
    }

    public function register(){
        $this->display();
    }

    public function pass(){
        $user = isset($_REQUEST['user'])?$_REQUEST['user']:'';
        $password1 = isset($_REQUEST['password1'])?$_REQUEST['password1']:'';
        $password2 = isset($_REQUEST['password2'])?$_REQUEST['password2']:'';
        if (empty($user))
            $this->error('用户名填写错误');
        if (empty($password1) || empty($password2))
            $this->error('密码填写错误');
        if ($password1 != $password2)
            $this->error('密码填写不一致');
        $password = md5($password1);
        $account = M('user');
        $condition = array(
            'user' => $user,
            'password' => $password,
            'rule' => 0,
            'status' => 0,
            'is_delete' => 1
        );
        if ($account->add($condition)){
            $this->success('注册成功，请联系管理员激活账号',U("index/login"));
        } else {
            $this->error('注册失败');
        }
    }
}